package org.example.controller;

import org.example.model.Major;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ ClassName: MajorController
 * @ Description：请求Major的接口
 * @ Modified By：
 * @ Version:
 * @ Author     ：LiYing
 * @ Date       ：Created in 16:16 2022/4/25
 */

@Controller
@RequestMapping(value = "/major")
public class MajorController {
    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "/id")
    public @ResponseBody
    Major id(HttpServletRequest request) {
        int id = Integer.parseInt(request.getParameter("id"));
        Major forObject = restTemplate.getForObject("http://EUREKA-SERVICE/major/id?id=" + id, Major.class);
        return forObject;
    }

    @RequestMapping(value = "/name")
    public @ResponseBody
    Major name(HttpServletRequest request) {
        String name = request.getParameter("name");
        Major forObject = restTemplate.getForObject("http://EUREKA-SERVICE/major/name?name=" + name, Major.class);
        return forObject;
    }

    @RequestMapping(value = "/academy")
    public @ResponseBody List<Major> academy(HttpServletRequest request) {
        String academy = request.getParameter("academy");
        List<Major> forObject = restTemplate.getForObject("http://EUREKA-SERVICE/major/academy?academy=" + academy, List.class);
        return forObject;
    }
}
